// pages/result/result.js
import Dialog from '@vant/weapp/dialog/dialog';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    result: "fail",
    type: "",
    num: 5,
    url: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      result: options.result,
      type: options.type,
      url: options.url
    })
    if (options.type == "weikuan") {
      this.minusNum()
    }
  },

  minusNum() {
    setTimeout(() => {
      this.setData({
        num: this.data.num - 1
      })
      if (this.data.num >= 1) {
        this.minusNum()
      }
    }, 1000)
  },

  toList() {
    let that = this
    if (this.data.type == "yushou" && this.data.result == "success") {
      wx.getSetting({ // 查询订阅消息
        withSubscriptions: true,
        success(res) {
          console.log(res.subscriptionsSetting)
          if (res.subscriptionsSetting['BVe_3zjQ7VKnbghH7oNXfsPcJAQRZIitS3gasq2UPSE'] == "reject") {
            Dialog.confirm({
                title: '温馨提示',
                message: '您没有开启订阅消息的权限，是否去设置？',
                confirmButtonText: '去设置',
              })
              .then(() => {
                wx.openSetting({
                  success(res) {}
                })
              })
              .catch(() => {
                wx.reLaunch({
                  url: that.data.url,
                })
              });
          } else {
            wx.requestSubscribeMessage({
              tmplIds: ['BVe_3zjQ7VKnbghH7oNXfsPcJAQRZIitS3gasq2UPSE'],
              success(res1) {
                console.log(res1)
                wx.reLaunch({
                  url: that.data.url,
                })
              }
            })
          }
        }
      })
    } else if (this.data.type == "weikuan" && this.data.result == "success") {
      wx.getSetting({ // 查询订阅消息
        withSubscriptions: true,
        success(res) {
          console.log(res.subscriptionsSetting)
          if (res.subscriptionsSetting['gzE2MtWOwgmt9F8VdRMMDM8wclsNKQsPoHf5CwLkOTs'] == "reject" || res.subscriptionsSetting['UAjyidlquF2Pr_XvZubo7X-rG7dNFffmTw8i_8qrV7k'] == "reject" || res.subscriptionsSetting['1zp0CIIl6-xexIqhHowT5_3luoTMMsupzJgs1DWq9Ls'] == "reject") {
            Dialog.confirm({
                title: '温馨提示',
                message: '您没有开启订阅消息的权限，是否去设置？',
                confirmButtonText: '去设置',
              })
              .then(() => {
                wx.openSetting({
                  success(res) {}
                })
              })
              .catch(() => {
                wx.reLaunch({
                  url: that.data.url,
                })
              });
          } else {
            wx.requestSubscribeMessage({
              tmplIds: ['gzE2MtWOwgmt9F8VdRMMDM8wclsNKQsPoHf5CwLkOTs', 'UAjyidlquF2Pr_XvZubo7X-rG7dNFffmTw8i_8qrV7k', '1zp0CIIl6-xexIqhHowT5_3luoTMMsupzJgs1DWq9Ls'],
              success(res1) {
                console.log(res1)
                wx.reLaunch({
                  url: that.data.url,
                })
              }
            })
          }
        }
      })
    } else {
      wx.reLaunch({
        url: this.data.url,
      })
    }
  },

  // 返回
  back() {
    wx.switchTab({
      url: '/pages/home/home',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})